package com.itboye.hutouben.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.presenter.UserPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.volley.ResultEntity;
import com.itboye.hutouben.web.WebActivity;

import java.util.Observable;
import java.util.Observer;

/**
 * 注册1
 */

public class RegisterOneActivity extends BaseActivity implements Observer {
    ImageView close_icon;// 返回图片
    TextView add_shap_title_tv;// 标题
    EditText resster_one_tel;
    TextView register_one_ok;

    TextView xieyi;

    UserPresenter userPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_one_activity);

        add_shap_title_tv.setText(R.string.plese_phone);

        userPresenter = new UserPresenter(this);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.xieyi://协议
                Intent intent01 = new Intent(this, WebActivity.class);
                intent01.putExtra("url", Const.xieyiUrl);
                intent01.putExtra("title", "用户协议");
                startActivity(intent01);
                break;
            case R.id.register_one_ok:
                if (resster_one_tel.getText().toString().length() != 11) {
                    ByAlert.alert("请输入正确的电话号码！");
                    return;
                }
                showProgressDialog("请稍后...", true);
                userPresenter.sendVerificationCode("+86", resster_one_tel.getText().toString(), 1 + "");

                break;
            default:
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == UserPresenter.send_code_success) {
                ByAlert.alert(entity.getData());
                Intent intent = new Intent(this, RegisterTwoActivity.class);
                intent.putExtra("phone", resster_one_tel.getText().toString());
                startActivity(intent);
                RegisterOneActivity.this.finish();
                Log.d("send_code_success", entity.getData() + "");
            }
            if (entity.getEventType() == UserPresenter.send_code_fail) {
                ByAlert.alert(entity.getData());
                Log.d("send_code_success", entity.getData() + "");

            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
